const fs = require("fs");
const Logger =class {
    static info(...msg){
        log('INFO',...msg);
    }
    static warn(...msg){
        log('WARN',...msg);
    }
    static error(...msg){
        log('ERROR',...msg);
    }
}
/**
 *
 * @param {string}level
 * @return string
 */
function parsedLevel(level){
    switch (level){
        case 'INFO':return 'color:green;font-weight:bold;';
        case 'WARN':return 'color:yellow;font-weight:bold;';
        case 'ERROR':return 'color:red;font-weight:bold;';
        default:
            return 'font-weight:bold;';
    }
}

function log(level,...msg){
    let color = parsedLevel(level);
    console.log(`%c[${level}] `,color,`${new Date().toISOString()} `,...msg)
}

// 打印日志到文件
const stdout = fs.createWriteStream("log/stdout.log",{flags:"a+"});
const stderr = fs.createWriteStream("log/stderr.log",{flags:"a+"});
const fileConsole = new console.Console(stdout,stderr);
function log2File(level,...msg){
    let color = parsedLevel(level);
    fileConsole.log(`%c[${level}] `,color,`${new Date().toISOString()} `,...msg);
}
const FileLogger =class {
    static info(...msg){
        log2File('INFO',...msg);
    }
    static warn(...msg){
        log2File('WARN',...msg);
    }
    static error(...msg){
        log2File('ERROR',...msg);
    }
}
function formatCurrentTime() {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的，所以要加1
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');

    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}
module.exports = {
    Logger,
    FileLogger
}